package com.tms.basic.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tms.basic.pojo.domain.Vehicle;
import com.tms.basic.pojo.req.VehicleQueryReq;
import com.tms.basic.pojo.vo.VehicleVO;
import com.tms.basic.service.VehicleService;
import com.tms.common.core.domain.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 车辆信息管理接口
 *
 * @author TMS
 * @since 2024-01-01
 */
@Tag(name = "车辆管理", description = "车辆信息的增删改查接口")
@RestController
@RequestMapping("/vehicle")
@RequiredArgsConstructor
public class VehicleController {

    private final VehicleService vehicleService;

    @Operation(summary = "分页查询车辆信息", description = "根据条件分页查询车辆信息")
    @PostMapping("/page")
    public R<IPage<VehicleVO>> getVehiclePage(@RequestBody VehicleQueryReq req) {
        return vehicleService.getVehiclePage(req);
    }

    @Operation(summary = "查询车辆列表", description = "根据条件查询车辆信息列表")
    @PostMapping("/list")
    public R<List<VehicleVO>> getVehicleList(@RequestBody VehicleQueryReq req) {
        return vehicleService.getVehicleList(req);
    }

    @Operation(summary = "根据ID查询车辆详情", description = "根据车辆ID查询车辆详细信息")
    @GetMapping("/{id}")
    public R<VehicleVO> getVehicleById(@Parameter(description = "车辆ID") @PathVariable Long id) {
        return vehicleService.getVehicleById(id);
    }

    @Operation(summary = "根据车牌号查询车辆", description = "根据车牌号查询车辆信息")
    @GetMapping("/license-plate/{licensePlate}")
    public R<VehicleVO> getVehicleByLicensePlate(@Parameter(description = "车牌号") @PathVariable String licensePlate) {
        return vehicleService.getVehicleByLicensePlate(licensePlate);
    }

    @Operation(summary = "新增车辆", description = "新增车辆信息")
    @PostMapping
    public R<Boolean> addVehicle(@Valid @RequestBody Vehicle vehicle) {
        return vehicleService.addVehicle(vehicle);
    }

    @Operation(summary = "修改车辆信息", description = "修改车辆信息")
    @PutMapping
    public R<Boolean> updateVehicle(@Valid @RequestBody Vehicle vehicle) {
        return vehicleService.updateVehicle(vehicle);
    }

    @Operation(summary = "删除车辆", description = "根据ID删除车辆信息")
    @DeleteMapping("/{id}")
    public R<Boolean> deleteVehicle(@Parameter(description = "车辆ID") @PathVariable Long id) {
        return vehicleService.deleteVehicle(id);
    }

    @Operation(summary = "批量删除车辆", description = "根据ID列表批量删除车辆信息")
    @DeleteMapping("/batch")
    public R<Boolean> deleteVehicleBatch(@RequestBody List<Long> ids) {
        return vehicleService.deleteVehicleBatch(ids);
    }

    @Operation(summary = "更新车辆状态", description = "更新车辆的状态信息")
    @PutMapping("/{id}/status/{status}")
    public R<Boolean> updateVehicleStatus(
            @Parameter(description = "车辆ID") @PathVariable Long id,
            @Parameter(description = "新状态") @PathVariable Integer status) {
        return vehicleService.updateVehicleStatus(id, status);
    }

    @Operation(summary = "查询即将到期的车辆", description = "查询保险或年检即将到期的车辆")
    @GetMapping("/expiring")
    public R<List<VehicleVO>> getExpiringVehicles(
            @Parameter(description = "提前天数，默认30天") @RequestParam(defaultValue = "30") Integer days) {
        return vehicleService.getExpiringVehicles(days);
    }

    @Operation(summary = "车辆状态统计", description = "根据状态统计车辆数量")
    @GetMapping("/status/count")
    public R<List<Map<String, Object>>> countVehicleByStatus(
            @Parameter(description = "机构ID") @RequestParam(required = false) Long organizationId) {
        return vehicleService.countVehicleByStatus(organizationId);
    }
} 